package com.example.yk

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.yk.adapter.GoodsAdapter
import com.example.yk.adapter.GroupAdapter
import com.example.yk.adapter.GroupChildAdapter
import com.example.yk.databinding.ActivityMainBinding
import com.example.yk.entity.GroupEntity
import com.example.yk.entity.MallGoodsInfoPageResponse
import com.example.zk3.mvvm.BaseActivity
import com.youth.banner.loader.ImageLoader

class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    val list = mutableListOf<GroupEntity>()
    val imglist = mutableListOf<String>()
    lateinit var groupAdapter: GroupAdapter

    val childlist = mutableListOf<GroupEntity>()
    lateinit var groupChildAdapter: GroupChildAdapter

    val wclist = mutableListOf<MallGoodsInfoPageResponse>()
    lateinit var wcAdapter:GoodsAdapter
    val jwlist = mutableListOf<MallGoodsInfoPageResponse>()
    lateinit var hwAdapter:GoodsAdapter
    override fun initData() {
        viewModel.groupSuccess.observe(this){
            if(it.success){
                //一级分组展示
                list.clear()
                list.addAll(it.datas)
                groupAdapter.notifyDataSetChanged()
                //二级分组展示
                val data = it.datas[0].child
                if(data!=null){
                    childlist.clear()
                    childlist.addAll(data)
                    groupChildAdapter.notifyDataSetChanged()
                }
            }
        }
        viewModel.bannerSuccess.observe(this){
            if(it.success){
                it.mallAdInfo.forEach {
                    imglist.add(it.imageUrl)
                }
                binding.banner.setImages(imglist)
                binding.banner.setImageLoader(object:ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        if(context!=null&&path!=null){
                            Glide.with(context).load(path).into(imageView!!)
                        }
                    }
                })
                binding.banner.start()
            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.success){
                wclist.clear()
                val wcdata = it.datas[0].mallGoodsInfoPageResponseList
                if(wcdata!=null){
                    wclist.addAll(wcdata)
                }
                wcAdapter.notifyDataSetChanged()

                jwlist.clear()
                val jwdata = it.datas[1].mallGoodsInfoPageResponseList
                if(wcdata!=null){
                    jwlist.addAll(jwdata)
                }
                hwAdapter.notifyDataSetChanged()
            }
        }

    }

    override fun initView() {
        groupAdapter = GroupAdapter(list)
        binding.groupRv.adapter = groupAdapter
        //水平滑动的线性布局
        binding.groupRv.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        viewModel.group()

        groupChildAdapter = GroupChildAdapter(childlist)
        binding.childRv.adapter = groupChildAdapter
        binding.childRv.layoutManager = GridLayoutManager(this,5)


        viewModel.banner("skl_app_home_banner")

        //一级分组的点击事件
        groupAdapter.setOnItemClickListener { adapter, view, position ->
            val data = list[position].child
            childlist.clear()
            if(data!=null){
                childlist.addAll(data)
            }
            groupChildAdapter.notifyDataSetChanged()
            binding.childRv.isVisible = false
        }
        groupAdapter.addChildClickViewIds(R.id.open)
        groupAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.open->{
                    val data = list[position].child
                    childlist.clear()
                    if(data!=null){
                        childlist.addAll(data)
                    }
                    groupChildAdapter.notifyDataSetChanged()
                    binding.childRv.isVisible = true
                }
            }

        }

        wcAdapter = GoodsAdapter(wclist)
        binding.wcRv.adapter = wcAdapter
        binding.wcRv.layoutManager = GridLayoutManager(this,2)

        hwAdapter = GoodsAdapter(jwlist)
        binding.hwRv.adapter = hwAdapter
        binding.hwRv.layoutManager = GridLayoutManager(this,2)

        viewModel.goods()



        hwAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("id",jwlist[position].id).navigation()
        }
        //跳转到搜索界面
        binding.et.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }
        //点击跳转到好物更多模块
        binding.tvHwMore.setOnClickListener {
            ARouter.getInstance().build("/app/MoreActivity").navigation()
        }


    }

    override fun getLayout(): Int  = R.layout.activity_main

    override fun getViewModel(): Class<MainViewModel>  = MainViewModel::class.java

}